package system.servlet;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.Vector;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import system.download.FileBusiness;

/**
 * Servlet implementation class for Servlet: Jw
 *
 */
 public class Jw extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
   static final long serialVersionUID = 1L;
   
    /* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public Jw() {
		super();
	}   	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	    doPost(request, response);
	}  	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	    request.setCharacterEncoding("UTF-8");
	    System.out.println(response.getCharacterEncoding());;
	    String code=request.getParameter("code");  
        HttpSession session = request.getSession();
        Vector vec = (Vector)session.getAttribute("list");
        HSSFWorkbook book = null;
        book = FileBusiness.getFile(vec, code,book);
        
        
        response.setHeader("Cache-Control", "No-cache");
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache,no-store,max-age=0");
        response.setDateHeader("Expires", 0);
        //response.setContentType("application/ms-excel;charset=GBK");
        
        /** 解决乱码*/
        response.setContentType("application/octet-stream");   
        byte[] b = code.getBytes("GBK");   
        code = new String(b,"8859_1");   
        response.setHeader("Content-Disposition","attachment;filename="+code);
        /** 解决乱码*/
        
        //code = URLEncoder.encode(code);
        System.out.println(response.getCharacterEncoding());;
        //response.setHeader("Content-disposition", "attachment; filename=\"" + code + "\"");
        ServletOutputStream out = response.getOutputStream();
        book.write(out);
        out = response.getOutputStream();
        book.write(out);
        out.flush();
        out.close();
	}   	  	    
}